package Server;

import java.io.IOException;
import java.net.ServerSocket;
import java.sql.SQLException;

/**
 * Main class of Server package. Used to start the ServerSocket, Database, 
 * @author PZK
 *
 */
public class Server {

	private static final int PORT = 12345;
	private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/psi";
	private static final String DATABASE_USERNAME = "root";
	private static final String DATABASE_PASSWORD = "";

	private ServerSocket serverSocket;
	private Database database;
	@SuppressWarnings("unused")
	private ClientHandler clientHandler; //for future reference.

	public Server() {

		try {
			serverSocket = new ServerSocket(PORT);
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
			System.exit(1);
		}

		try {
			database = new Database(DATABASE_URL, DATABASE_USERNAME,
					DATABASE_PASSWORD);
		} catch (SQLException e) {
			System.out.println("HINT: Turn on the MySQL server.");
			System.exit(1);
		} catch (ClassNotFoundException e) {
			System.out.println("ERROR: MySQL driver for java connector is not installed to the library!");
			System.exit(1);
		}

		clientHandler = new ClientHandler(serverSocket, database);

	}

	public static void main(String[] args) {

		new Server();

	}
}
